package com.xzx.mybatis.config;

import com.zaxxer.hikari.HikariDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.datasource.DataSourceFactory;

import javax.sql.DataSource;
import java.util.Properties;

/**
 *
 * @author xinzhixuan
 * @version 1.0
 * @date 2025/8/26 12:01
 */
@Slf4j
public class HikariDataSourceFactory implements DataSourceFactory {
    private static final String DRIVER_PROPERTY_PREFIX = "driver.";

    protected DataSource dataSource;

    public HikariDataSourceFactory() {
        this.dataSource = new HikariDataSource();
    }

    @Override
    public void setProperties(Properties props) {
        log.info("====props:{}", props);
        HikariDataSource hikariDataSource = (HikariDataSource) dataSource;
        hikariDataSource.setDriverClassName(props.getProperty("driver"));
        hikariDataSource.setJdbcUrl(props.getProperty("url"));
        hikariDataSource.setUsername(props.getProperty("username"));
        hikariDataSource.setPassword(props.getProperty("password"));
    }

    @Override
    public DataSource getDataSource() {
        return this.dataSource;
    }
}
